package com.wiim.adapter;

import java.util.ArrayList;
import java.util.List;

import com.wiim.common.ConstantValue;
import com.wiim.common.Utilities;
import com.wiim.connector.RestClient;
import com.wiim.model.ExchangeAwardData;
import com.wiim.model.ShopData;
import com.wiim.nhapnhap.CustomFragmentActivity;
import com.wiim.nhapnhap.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DoiThuongAdapter extends BaseAdapter {
	// private ArrayList<ExchangeAwardData> listData;
	private ArrayList<ShopData> listData;
	private Activity ac;
	private List<Integer> pic;

	// private String service_url = "common/getfilestream/";

	static class ViewHolder {
		ImageView imgPics;
		TextView tvDetail;
		RelativeLayout rl;
	}

	public DoiThuongAdapter(final Activity _ac, ArrayList<ShopData> _list, List<Integer> listAdd2) {
		this.listData = _list;
		this.ac = _ac;
		this.pic = listAdd2;
		// imageLoader = new ImageLoader(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listData.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return listData.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(ac);
			convertView = inflater.inflate(R.layout.doithuong_item, null);
			holder.imgPics = (ImageView) convertView.findViewById(R.id.imgPics);
			holder.tvDetail = (TextView) convertView.findViewById(R.id.tvDetail);
			holder.rl = (RelativeLayout) convertView.findViewById(R.id.rl);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		LayoutParams lp = holder.rl.getLayoutParams();
		lp.width = (int) (Utilities.getGlobalVariable(ac).device_width / 1.2);
		lp.height = (int) (Utilities.getGlobalVariable(ac).device_height / 10.5);
		holder.rl.setLayoutParams(lp);
		holder.rl.setBackgroundResource(pic.get(position));

		holder.imgPics.setVisibility(View.GONE);
		holder.tvDetail.setText(listData.get(position).ShopName);

		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((CustomFragmentActivity) ac).setPassValue("SHOPID", listData.get(position).ShopID);
				((CustomFragmentActivity) ac).replaceFragment(ConstantValue.STORE_DETAIL_FRAGMENT, false);
			}
		});

		// ((CustomFragmentActivity)ac).imageLoader2.displayImage(RestClient.apiUrl(service_url
		// + listData.get(position).ShopID), holder.imgPics,
		// ((CustomFragmentActivity) ac).options);

		return convertView;
	}
	// public static String format(String formatText, Object param0) {
	// return format(formatText, new Object[] { param0 });
	// }

}
